package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.sys.entity.User;
import com.example.sys.service.UserService;
import com.example.util.NetworkUtil;
import com.example.util.UserRegisteAndLogin;
import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Wrapper;
import java.time.LocalDateTime;


@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping({"/", "/index"})
    public String index() {
        return "/index";
    }

    @GetMapping("/login")
    public String login() {
        return "/login";
    }

    @PostMapping("/login")
    public String postLogin(User user,HttpServletRequest request) {
        //用户信息bean
        System.out.println(user.getLoginName());
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginName(), user.getPassword());
        try {
            //验证
            subject.login(token);
            user.setLoginIp(NetworkUtil.getIP(request));
            //登陆成功后的处理逻辑：
            user.setLoginDate(LocalDateTime.now());
            userService.updateUserLoginInfo(user);
            System.out.println("登陆成功");
            return "index";
        } catch (Exception e) {
            //登陆失败后的处理逻辑：
            System.out.println("用户名或密码错误");
            return "login";
        }
    }

    /***
     * 用户注册POST
     * @return
     */
    @RequestMapping("/userInfo/register")
    public String register(User user, HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        String str = NetworkUtil.getIP(request);

        if (user.getLoginName() != null) {
            String loginName = user.getLoginName();
            String password = user.getPassword();
            String[] strs = UserRegisteAndLogin.encryptPassword(loginName, password);
            user.setSalt(strs[1]);
            user.setPassword(strs[0]);
            if (str != null) {
                user.setLoginIp(str);
            }
            user.setLoginDate(LocalDateTime.now());
            user.setCreateTime(LocalDateTime.now());
//          向数据库写入
            userService.save(user);

//          注册成功后直接登陆
            User userLogin = new User();
            userLogin.setLoginName(loginName);
            userLogin.setPassword(password);
            return UserRegisteAndLogin.userLogin(userLogin, model);
        }
        return "register";
    }

    @RequestMapping("/403")
    public String unauthorizedRole() {
        System.out.println("------没有权限-------");
        return "403";
    }


}
